package com.coolwen.controller;

import com.alibaba.fastjson.JSON;
import com.coolwen.entity.LoginUser;
import com.coolwen.entity.User;
import com.coolwen.entity.UserException;
import com.coolwen.service.LoginUserService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;


import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by user on 2016/5/18.
 */
@Controller
@RequestMapping("/login")
@SessionAttributes("lgUser")
public class LoginController {

    private Logger logger = Logger.getLogger(getClass());

    @Resource
    private LoginUserService loginUserService;

    @RequestMapping(value = "/do_login", method = RequestMethod.GET)
    public String login(Model model) {
//        LoginUser loginUser = new LoginUser();

        model.addAttribute(new LoginUser());
        return "login/login";
    }

    @RequestMapping(value = "/do_login", method = RequestMethod.POST)
    public String login(@Validated LoginUser loginUser, BindingResult br, Model model) {

        if (br.hasErrors()) {
            //如果有错误直接跳转到add视图
            return "login/login";
        }
        String loginName = loginUser.getLoginName();
        String loginPassword = loginUser.getLoginPassword();
        logger.info("查找用户名:" + loginName + "查找用密码:" + loginPassword);
        if (loginUserService.selectUserByUserName(loginName) == null || !loginUserService.selectUserByUserName(loginName).getLoginPassword().equals(loginPassword)) {
            logger.info("查找用户为:" + JSON.toJSON(loginUserService.selectUserByUserName(loginName)));
            throw new UserException("用户名和密码错误！");
        }
        logger.info("查找用户结果为:" + JSON.toJSON(loginUserService.selectUserByUserName(loginName)));
        model.addAttribute("loginUser", loginUser);
        //记录登录时间，设置登录标志为true
        loginUser=loginUserService.selectUserByUserName(loginName);
        loginUser.setLoginTime(refFormatNowDate());
        loginUser.setLoginSign(true);
        logger.info("更新用户为:" + JSON.toJSON(loginUser));
        loginUserService.updateByPrimaryKeySelective(loginUser);

        return "redirect:/user/bootstrapTest1";

    }

    @RequestMapping(value = "/logout")
    public String logout(Model model,HttpSession session){
        model.asMap().remove("lgUser");
        session.invalidate();
        return "redirect:/login/do_login";

    }


    public Date refFormatNowDate() {
        Date nowTime = new Date(System.currentTimeMillis());
        SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy-MM-dd");
        String retStrFormatNowDate = sdFormatter.format(nowTime);
        return nowTime;
    }

}
